package com.hyyt.shopjsp.order.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.order.pojo.ShopOrders;
import com.hyyt.shopjsp.order.pojo.ShopShipping;
import com.hyyt.shopjsp.order.service.IShopOrdersService;
import com.hyyt.shopjsp.order.service.IShopShippingService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/06/15 0024下午 3:56.
 */

@Controller
@RequestMapping("/back/shipping")
public class ShopShippingController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopShippingController.class);
	private String basePath = "/back/shop/shipping/";
	@Resource
	private IShopOrdersService shopOrdersService;//订单service接口
	@Resource
	private IShopShippingService shopShippingService;//发货详情service接口

	/**
	 * 跳转订单日志列表页面
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoShippingPage")
	public String gotoShippingPage(Map<String, Object> param){
		List<ShopOrders> ordersList = shopOrdersService.queryAllShopOrders();
		param.put("ordersList", ordersList);
		return basePath+"shippingList";
	}
	/**
	 * 订单日志列表
	 * @param page
	 * @param response
	 * @param request
	 */
	@RequestMapping(value="listShipping",method=RequestMethod.POST)
	@ResponseBody
	public void listShipping(Page page, HttpServletResponse response, HttpServletRequest request){
		String shippingSn = request.getParameter("title");//发货单号
		String ordersNo = request.getParameter("ordersNo");//发货单号
		String shopInfoType = request.getParameter("shopInfoType");
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(shippingSn)){
			params.put("shippingSn", shippingSn.trim());
		}
		if(Utils.objectIsNotEmpty(ordersNo)){
			params.put("ordersNo", ordersNo.trim());
		}
		if(Utils.objectIsNotEmpty(shopInfoType)){
			params.put("shopInfoType", shopInfoType);
		}
		int totalRecordCount =shopShippingService.countShopShipping(params);
		 page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		 List<ShopShipping> shippingList  =shopShippingService.queryShippingList(params, page.getPageRecordBeginIndex(), page.getPageSize());
		 params.clear();
		 params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		 params.put("rows", shippingList);// rows键 存放每页记录 list
		 ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
     * 根据id获得对象
     *
     * @param shippingId
     */
    @RequestMapping(value = "getShippingObject", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg getShippingObject(Integer shippingId) {
    	Map<String, Object> params=new HashMap<>();
		if (Utils.objectIsNotEmpty(shippingId)) {
			ShopShipping shopShipping = shopShippingService.loadShopShipping(shippingId);
			if(Utils.objectIsNotEmpty(shopShipping)){
				ShopOrders shopOrders = shopOrdersService.loadShopOrders(shopShipping.getOrdersId());//订单信息
				params.put("shopOrders", shopOrders);
			}
			params.put("shopShipping", shopShipping);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(params));
		}
		return ResultMsg.create(false);
	}
	
    /**
     * 保存或修改发货详情
     * @param shopShipping
     * @return
     */
    @RequestMapping(value="saveOrUpdateShipping",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateShipping(ShopShipping shopShipping){
        boolean flag=false;
        Map<String,Object> update_map=new HashMap<>();
        //判断发货对象是否为空
    	if(Utils.objectIsNotEmpty(shopShipping)){
    	    //判断发货Id是否为空
    		if(Utils.objectIsNotEmpty(shopShipping.getShippingId())){
                update_map.put("shippingId",shopShipping.getShippingId()); //发货Id
                update_map.put("deliverySn",shopShipping.getDeliverySn()); //页面中传过来的发货编号
                int result=shopShippingService.updateDeliverySn(update_map);//根据Id更新发货编号
                if(result!=0){
                    flag=true;
                }
    		}
    	}
    	return ResultMsg.create(flag);
    }
    
    /**
     * 删除发货详情
     * @param shippingId
     * @return
     */
    @RequestMapping(value="deleteShipping",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteShipping(String shippingId){
        boolean flag=false;
    	if(Utils.objectIsNotEmpty(shippingId)){
    		shopShippingService.deleteShopShipping(Integer.parseInt(shippingId));
            flag=true;
    	}
    	return ResultMsg.create(flag);
    }
}
